<?php
namespace App\Logic\SalesReport;

use App\Models\Basics\ExchangeRate;
use App\Models\SalesReport\PaymentDetail as TPaymentDetail;
use App\Models\SalesReport\PaymentDetailJP;
use Illuminate\Support\Str;

class PaymentDetailForJP
{
    /**
     * 按去年同期汇率核算当前期月报,type = 1
     * @param $date
     * @param $lastYearDate
     * @return void
     */
    public static function exe1Payment($date,$lastYearDate){
        $rate_odds = ExchangeRate::getRateOddsToRMB('JPY',strtotime($lastYearDate));//获取去年同期汇率
        $n = 0;
        $commonFieldKeys = array_keys(PaymentDetailJP::$_commonFields);
        TPaymentDetail::query()
            ->where('report_date' ,$date)
            ->where('area_id',6)
            ->orderBy('id')
            ->chunk(100, function ($ss) use(&$n,$rate_odds,$commonFieldKeys){
                foreach ($ss as $_ss){
                    $model = self::needUpdateField($_ss,$rate_odds,$commonFieldKeys,1);
                    PaymentDetailJP::query()->create($model);
                    $n++;
                }
            });
        echo '添加成功：'.$n.'条'.PHP_EOL;
    }

    public static function needUpdateField($model,$rateOdds,$commonFieldKeys,$type): array
    {
        $arr1 = [
            'type' => $type,
            'report_date' => $model->report_date,
            'account_id' => $model->account_id,
            'seller_id' => $model->seller_id,
            'seller_sku' => $model->seller_sku,
            'psku_id' => $model->psku_id,
        ];
        $arr2 = [];
        foreach ($commonFieldKeys as $key) {
            if($key == 'rate_odds_same_period'){
                $arr2['rate_odds_same_period'] = $rateOdds;//同期倍率
            }elseif(Str::endsWith($key,'_old')){
                if(in_array($key,['earning_rmb_old','profit_rmb_old'])){
                    continue;
                }else{
                    $newKey = str_replace('_rmb_old','',$key);
                    $arr2[$key] = round($model->{$newKey} * $rateOdds,3);//同期倍率费用RMB
                }
            }else{
                $arr2[$key] = $model->{$key};
            }
        }
        $profit_rmb_old = round($arr2['sales_amount_rmb_old'] +
            $arr2['ied_rmb'] -
            $arr2['t_purchase_price_rmb'] -
            $arr2['t_shipment_price_rmb'] +
            $arr2['refund_amount_rmb_old'] +
            $arr2['selling_fees_rmb_old'] +
            $arr2['fba_fees_rmb_old'] +
            $arr2['p_thfba_rmb_old'] -
            $arr2['ms_fee_rmb_old'] -
            $arr2['lts_fee_rmb_old'] +
            $arr2['p_icfee_rmb_old'] -
            $arr2['rm_fee_rmb_old'] +
            $arr2['rebates_rmb_old'] -
            $arr2['spend_rmb_old'] -
            $arr2['reimburse_rmb'] -
            $arr2['reimburse_os_rmb'] -
            $arr2['reimburse_profit_rmb'] +
            $arr2['cplan_fee_rmb_old'] -
            $arr2['rod_rmb'],3);
        $arr3 = [
            'profit_rmb_old' => $profit_rmb_old,
            'earning_rmb_old' => round($arr2['t_purchase_price_rmb'] +
                $arr2['t_shipment_price_rmb'] +
                $profit_rmb_old,3)
        ];
        return $arr1 + $arr2 + $arr3;
    }
    /**
     * 按当前期的汇率去核算去年同期月报,type = 2
     * @param $date
     * @param $lastYearDate
     * @return void
     */
    public static function exe2Payment($date,$lastYearDate){
        $rate_odds = ExchangeRate::getRateOddsToRMB('JPY',strtotime($date));//获取当前期汇率
        $n = 0;
        $commonFieldKeys = array_keys(PaymentDetailJP::$_commonFields);
        TPaymentDetail::query()
            ->where('report_date' ,$lastYearDate)//核算去年同期月报
            ->where('area_id',6)
            ->orderBy('id')
            ->chunk(100, function ($ss) use(&$n,$rate_odds,$commonFieldKeys){
                foreach ($ss as $_ss){
                    $model = self::needUpdateField($_ss,$rate_odds,$commonFieldKeys,2);
                    PaymentDetailJP::query()->create($model);
                    $n++;
                }
            });
        echo '添加成功：'.$n.'条'.PHP_EOL;
    }
}
